package com.unitymarvel.harmonyos.projects.findme.slice;

import com.unitymarvel.harmonyos.projects.findme.MainAbility;
import com.unitymarvel.harmonyos.projects.findme.ResourceTable;
import com.unitymarvel.harmonyos.projects.findme.common.GameRecord;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;

import java.util.List;
///////////////////////////////////////////////////
// 项目的详细实现过程：https://ke.qq.com/course/3677276?tuin=a22a65ce
// 关注微信公众号和B站，获取更多鸿蒙学习资源
// 微信公众号：极客起源
// B站：https://space.bilibili.com/477001733
//
// 作者:李宁（蒙娜丽宁）
//
// 版本: 1.0
// Copyright (C), 2001-2022, 李宁
// 程序被版权法保护
//
///////////////////////////////////////////////////
public class SettingsAbilitySlice extends AbilitySlice {
    public static int countDown = 60;           // 游戏倒计时，默认60秒
    public static int scoreIncrement = 5;       // 分数增量，默认是5分
    public static int randomInterval = 2000;    // 每次参数随机字符的时间间隔，默认2000毫秒
    public static String user = "unknown";      // 当前用户

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_settings);
        TextField textFieldCountDown = (TextField)findComponentById(ResourceTable.Id_textfield_countdown);
        textFieldCountDown.setText(String.valueOf(countDown));
        textFieldCountDown.addTextObserver(new Text.TextObserver(){
            @Override
            public void onTextUpdated(String s, int i, int i1, int i2) {
                try {
                    countDown = Integer.parseInt(s);
                }catch (NumberFormatException e) {

                }
            }
        });

        TextField textFieldScoreIncrement = (TextField)findComponentById(ResourceTable.Id_textfield_score_increment);
        textFieldScoreIncrement.setText(String.valueOf(scoreIncrement));
        textFieldScoreIncrement.addTextObserver(new Text.TextObserver(){
            @Override
            public void onTextUpdated(String s, int i, int i1, int i2) {
                try {
                    scoreIncrement = Integer.parseInt(s);
                }catch (NumberFormatException e) {

                }
            }
        });

        TextField textFieldRandomInterval = (TextField)findComponentById(ResourceTable.Id_textfield_random_interval);
        textFieldRandomInterval.setText(String.valueOf(randomInterval));
        textFieldRandomInterval.addTextObserver(new Text.TextObserver(){
            @Override
            public void onTextUpdated(String s, int i, int i1, int i2) {
                try {
                    randomInterval = Integer.parseInt(s);
                }catch (NumberFormatException e) {

                }
            }
        });

        TextField textFieldCurrentUser = (TextField)findComponentById(ResourceTable.Id_textfield_current_user);
        textFieldCurrentUser.setText(user);
        textFieldCurrentUser.addTextObserver(new Text.TextObserver() {
            @Override
            public void onTextUpdated(String s, int i, int i1, int i2) {
                try {
                    user = s;
                }catch (Exception e) {

                }
            }
        });

        Button buttonShowRecords = (Button)findComponentById(ResourceTable.Id_button_show_records);
        buttonShowRecords.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
               /* List<GameRecord> records = MainAbility.dataService.getGameRecords();
                for(GameRecord record:records) {
                    System.out.println("用户名：" + record.user);
                    System.out.println("积分：" + record.score);
                    System.out.println("记录时间：" + record.time);
                }

                */
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder().withAction("action.findme.gamerecords").build();
                intent.setOperation(operation);
                startAbility(intent);
            }
        });

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
